<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/2/28
 * Time: 19:40
 */

namespace app\common\cachemodel;

use app\common\cachemodel\Orders as OrdersCacheModel;
use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

/**
 * 教学课程相关模型
 * Class ZhunleCourse
 * @package app\common\model
 */
class ZhunleCourse extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_course';
    protected $cacheModel = 'course.course_id';

    /**
     * 获取所有上架的课程基本信息(分页)
     * @param array $map
     * @return array|bool
     * @throws \Exception
     */
    public function getAllCourseInfo()
    {
        return $this->getListPage(0, 'id', ['id', 'gid', 'is_recommended', 'title', 'thumb', 'video_thumb', 'description', 'base_num']);
    }


    /**
     * 批量获取视频缓存.根据商品id
     * @param $gids
     * @param array $field
     * @param int $type 1=>根据视频获取视频,2=>根据商品id获取视频
     * @return array
     * @throws \Exception
     */
    public function batchGetInfoByGIds($gids, $field = [])
    {
        $this->cacheModel = 'course.course_gid';
        $gids = is_array($gids) ? $gids : explode(',', $gids);

        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->batchGetCache($gids, function ($gids)
            {
                return $this->getInfoByGIdsInData($gids);

            }, $field, 'list_details');

        return $data;
    }

    /**
     * 从数据库批量获取课程信息,根据课程id
     * @param $ids
     * @return array|null
     */
    public function getInfoByIdsInData($ids, $type, $sortType = 'ASC')
    {
        $ids = is_array($ids) ? $ids : explode(',', $ids);

        $field = 'id,gid,sort,is_recommended,base_num,title,thumb,video_thumb,description,introduction,create_time';
        if ($ids)
        {
            try
            {
                $order = $this->where(['id' => ['in', $ids]])->order('id', $sortType)->column($field, 'id');
            }
            catch (\Exception $e)
            {
                $order = null;
            }
        } else
        {
            try
            {
                $order = $this->order(['id' => $sortType])->column($field, 'id');
            }
            catch (\Exception $e)
            {
                $order = null;
            }
        }


        return $order;
    }

    /**
     * 从数据库批量获取课程信息,根据商品id
     * @param $ids
     * @return array|null
     */
    public function getInfoByGIdsInData($ids)
    {
        $ids = is_array($ids) ? $ids : explode(',', $ids);
        $field = 'id,gid,sort,is_recommended,base_num,title,thumb,video_thumb,description,introduction,create_time';
        try
        {
            $order = $this->where(['gid' => ['in', $ids]])->order('id', 'DESC')->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $order = null;
        }

        return $order;
    }

    /**
     * 获取视频解锁状态
     * @param $uid
     * @param $gid
     * @return bool
     * @throws \Exception
     */
    public static function getVideoUnlockStatus($uid, $gid = 0)
    {
        $res['status'] = false;
        $OrdersCacheModel = new OrdersCacheModel();
        $prepaids = $OrdersCacheModel->PrepaidUnpaidOrder($uid, 0, 6, 1);
        $video_order = [];
        foreach ($prepaids as $prepaid)
        {
            if (isset($prepaid['video_data']))
            {
                $video_order[] = current($prepaid['video_data']);
            }
        }
        $video_gids = $video_order ? array_column($video_order, 'gid') : [];
        $awards = (new AwardGoods())->getUserAwardFromCache($uid, 2);
        $awards_gids = $awards ? array_column($awards, 'gid') : [];
        $res['gids'] = array_merge($video_gids, $awards_gids);
        if (in_array($gid, $res['gids']))
        {
            $res['status'] = true;
        }
        return $res;
    }
}
